package com.example.newsdaily.slice;

import com.alibaba.fastjson.JSON;
import com.example.newsdaily.MyApplication;
import com.example.newsdaily.ResourceTable;
import com.example.newsdaily.entity.NewsDetail;
import com.example.newsdaily.entity.TCollection;
import com.example.newsdaily.utl.ContainUtil;
import com.example.newsdaily.utl.HttpClientUtil;
import com.example.newsdaily.utl.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Image;
import ohos.agp.components.Text;

public class NewsDetailAbilitySlice extends AbilitySlice {
    Long news_id = new Long(-1);
    Image head_image;
    boolean terFlag = false;
    boolean isCollected = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_news_detail);

        // 获取新闻id
        news_id = intent.getLongParam("newsid", -1);
        terFlag = intent.getBooleanParam("ter", false);

        // 检查收藏状态
        collectionCheck();
        // 获取新闻详情
        initDetails();

        // 设置返回按钮
        findComponentById(ResourceTable.Id_back).setClickedListener(component -> back());

        //设置收藏
        findComponentById(ResourceTable.Id_collect).setClickedListener(component -> collect());

        head_image = (Image) findComponentById(ResourceTable.Id_details_head);
        head_image.setCornerRadius((float) head_image.getWidth() / 2);
    }

    private void initDetails() {
        new Thread(
                () -> {
                    String result = HttpClientUtil.doGet(ContainUtil.HOST +
                            ContainUtil.FIND_NEWS_DETAILS_BY_ID + "?id=" + news_id);
                    NewsDetail newsDetail = JSON.parseObject(result, NewsDetail.class);

                    if (newsDetail == null) {
                        terminateAbility();
                        return;
                    }
                    getUITaskDispatcher().asyncDispatch(
                            () -> {
                                ((Text) findComponentById(ResourceTable.Id_details_title)).setText(newsDetail.getTitle());
                                ((Text) findComponentById(ResourceTable.Id_details_editor)).setText(newsDetail.getEditor());
                                ((Text) findComponentById(ResourceTable.Id_details_details)).setText(ContainUtil.detailTexts.get(newsDetail.getTitle()));
                                ((Image) findComponentById(ResourceTable.Id_details_head)).setPixelMap(ContainUtil.detail_pic_id[newsDetail.getHead_img_id()]);
                                ((Image) findComponentById(ResourceTable.Id_details_pic)).setPixelMap(ContainUtil.detail_pic_id[newsDetail.getPic_id()]);
                                //((Image) findComponentById(ResourceTable.Id_collect)).setPixelMap(ResourceTable.Media_isCollected);
                            }
                    );

                }
        ).start();
    }

    private void collectionCheck() {
        new Thread(
                () -> {
                    String result = HttpClientUtil.doGet(ContainUtil.HOST +
                            ContainUtil.COLLECTION_CHECK + "?tUser=" + MyApplication.tuser.getId() + "&TBriefNews=" + news_id);
                    getUITaskDispatcher().asyncDispatch(
                            () -> {
                                if (result != null && result.charAt(1) != ']') {
                                    ((Image) findComponentById(ResourceTable.Id_collect)).setPixelMap(ResourceTable.Media_isCollected);
                                    isCollected = true;
                                } else {
                                    ((Image) findComponentById(ResourceTable.Id_collect)).setPixelMap(ResourceTable.Media_collect);
                                    isCollected = false;
                                }
                            }
                    );
                }
        ).start();
    }

    private void back() {
        terminateAbility();
    }

    private void collect() {
        new Thread(
                () -> {
                    if(!isCollected) {
                        String result = HttpClientUtil.doGet(ContainUtil.HOST +
                                ContainUtil.ADD_COLLECTION + "?tUser=" + MyApplication.tuser.getId() + "&TBriefNews=" + news_id);
                        this.getUITaskDispatcher().syncDispatch(
                                () -> {
                                    if (result != null) {
                                        ToastUtil.makeToast(this, "收藏成功", ToastUtil.TOAST_LONG);
                                        ((Image) findComponentById(ResourceTable.Id_collect)).setPixelMap(ResourceTable.Media_isCollected);
                                        isCollected = true;
                                    } else {
                                        ToastUtil.makeToast(this.getContext(), "收藏失败", ToastUtil.TOAST_LONG);
                                        ((Image) findComponentById(ResourceTable.Id_collect)).setPixelMap(ResourceTable.Media_collect);
                                        isCollected = false;
                                    }
                                }
                        );
                    } else {
                        String result = HttpClientUtil.doGet(ContainUtil.HOST +
                                ContainUtil.COLLECTION_ID_CHECK + "?tUser=" + MyApplication.tuser.getId() + "&TBriefNews=" + news_id).trim();
                        Integer id = Integer.parseInt(result);
                        HttpClientUtil.doGet(ContainUtil.DELETE_COLLECTION_NEW + "?id=" + id);
                        this.getUITaskDispatcher().syncDispatch(
                                () -> {
                                    ((Image) findComponentById(ResourceTable.Id_collect)).setPixelMap(ResourceTable.Media_collect);
                                    isCollected = false;
                                    ToastUtil.makeToast(this, "取消收藏成功", ToastUtil.TOAST_LONG);
                                    if (terFlag) {
                                        terminateAbility();
                                    }
                                }
                        );
                    }

                }
        ).start();

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
